package com.xuchaotong.utils.bean;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;

/**
 * @author EASON.TONG
 * @create 2021/06/11
 **/
public class BeanUtils {


    public static <T> T copy(Object source, Class<T> targetClass) {
        return BeanUtil.copyProperties(source, targetClass);
    }

    public static void copy(Object source, Object target, boolean ignoreNullValue) {
        if (ignoreNullValue) {
            CopyOptions copyOptions = CopyOptions.create().ignoreNullValue();
            BeanUtil.copyProperties(source, target, copyOptions);
        } else {
            BeanUtil.copyProperties(source, target);
        }
    }


    public static <T> T merge(Object source, Object target, Class<T> beanClass) {
        T obj = BeanUtil.copyProperties(target, beanClass);
        BeanUtil.copyProperties(source, obj, CopyOptions.create().ignoreNullValue());

        return obj;
    }

    public static <T> T merge(Object source, Object target, Class<T> beanClass, String... ignoreProperties) {
        T obj = BeanUtil.copyProperties(target, beanClass);
        BeanUtil.copyProperties(source, obj, CopyOptions.create().ignoreNullValue().setIgnoreProperties(ignoreProperties));

        return obj;
    }

}
